package com.example.mybatisplusdemo.web.controller;

import com.example.mybatisplusdemo.model.domain.Flight;
import com.example.mybatisplusdemo.model.domain.ItineraryRequest;
import com.example.mybatisplusdemo.model.domain.SegmentRequest;
import com.example.mybatisplusdemo.model.dto.SmartCombinationDTO;
import com.example.mybatisplusdemo.service.FlightSearchService;
import com.example.mybatisplusdemo.service.combination.FlightCombinationService;
import com.example.mybatisplusdemo.common.JsonResponse;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/smart")
public class SmartFlightController {

    private final FlightSearchService flightSearchService;
    private final FlightCombinationService combinationService;

    public SmartFlightController(
            FlightSearchService flightSearchService,
            FlightCombinationService combinationService) {
        this.flightSearchService = flightSearchService;
        this.combinationService = combinationService;
    }

    /**
            * 获取智能航班组合
     *
             * @param request 行程请求参数
     * @return 智能组合结果
     */
    @PostMapping("/combinations")
    public JsonResponse<Object> getSmartCombinations(
            @RequestBody ItineraryRequest request) {
        try {
            // 1. 验证请求参数
            validateRequest(request);

            // 2. 获取基础航班列表
            List<Flight> flights = flightSearchService.searchFlights(request);

            // 3. 生成智能组合
            List<SmartCombinationDTO> combinations =
                    combinationService.findSmartCombinations(flights, request);

            // 4. 返回成功响应
            return JsonResponse.success(combinations, "成功获取智能航班组合");
        } catch (IllegalArgumentException e) {
            // 参数验证失败
            return JsonResponse.failure(e.getMessage()).setCode(400);
        } catch (Exception e) {
            // 其他异常处理
            return JsonResponse.failure("获取智能航班组合失败: " + e.getMessage())
                    .setCode(500)
                    .setException(e);
        }
    }

    /**
            * 验证请求参数
     */
    private void validateRequest(ItineraryRequest request) {
        if (request == null) {
            throw new IllegalArgumentException("请求参数不能为空");
        }

        if (request.getSegments() == null || request.getSegments().isEmpty()) {
            throw new IllegalArgumentException("必须提供行程段信息");
        }

        // 验证每个行程段
        for (SegmentRequest segment : request.getSegments()) {
            if (segment.getOriginAirportCode() == null ||
                    segment.getOriginAirportCode().isEmpty()) {
                throw new IllegalArgumentException("出发机场代码不能为空");
            }

            if (segment.getDestinationAirportCode() == null ||
                    segment.getDestinationAirportCode().isEmpty()) {
                throw new IllegalArgumentException("到达机场代码不能为空");
            }

            if (segment.getDepartureDate() == null) {
                throw new IllegalArgumentException("出发日期不能为空");
            }
        }
    }
}